package com.bjpowernode.workbench.web.service.impl;

import com.bjpowernode.commons.contants.Contants;
import com.bjpowernode.commons.utils.DateUtils;
import com.bjpowernode.settings.web.domain.User;
import com.bjpowernode.workbench.web.domain.*;
import com.bjpowernode.workbench.web.mapper.ClueMapper;
import com.bjpowernode.workbench.web.mapper.ContactsMapper;
import com.bjpowernode.workbench.web.mapper.CustomerMapper;
import com.bjpowernode.workbench.web.mapper.TranRemarkMapper;
import com.bjpowernode.workbench.web.service.ClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
public class ClueServiceImpl implements ClueService {
    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private ContactsMapper contactsMapper;

    @Autowired
    private ClueRemarkServiceImpl clueRemarkService;

    @Autowired
    private CustomerRemarkServiceImpl customerRemarkService;

    @Autowired
    private ContactsRemarkServiceImpl contactsRemarkService;

    @Autowired ClueActivityRelationServiceImpl clueActivityRelationService;

    @Autowired
    private ContactsActivityRelationServiceImpl contactsActivityRelationService;

    @Autowired
    private TranServiceImpl tranService;

    @Autowired
    private TranRemarkMapper tranRemarkMapper;

    @Override
    public int insertClue(Clue clue) {
        return clueMapper.insertClue(clue);
    }

    @Override
    public List<Clue> selectClueForPageByConditions(Clue clue) {
        return clueMapper.selectClueForPageByConditions(clue);
    }

    @Override
    public int selectClueCountByCondition(Clue clue) {
        return clueMapper.selectClueCountByCondition(clue);
    }

    @Override
    public int deleteClueById(String[] id) {
        return clueMapper.deleteClueById(id);
    }

    @Override
    public int updateClueByClue(Clue clue) {
        return clueMapper.updateClueByClue(clue);
    }

    @Override
    public Clue selectClueById(String id) {
        return clueMapper.selectClueById(id);
    }

    @Override
    public Clue selectRealClueById(String id) {
        return clueMapper.selectRealClueById(id);
    }

    @Override
    public int deleteClueByClueId(String clueId) {
        return clueMapper.deleteClueByClueId(clueId);
    }

    @Override
    public void saveConvert(Map<String, Object> map) {
        User user = (User)map.get(Contants.SESSION_USER);
        String isCreateTran = (String)map.get("isCreateTran");
        //        客户的转换
        String clueId = (String) map.get("clueId");
        Clue clue  = clueMapper.selectRealClueById(clueId);
        Customer customer = new Customer();
        customer.setId(UUID.randomUUID().toString().replaceAll("-",""));
        customer.setOwner(user.getId());
        customer.setName(clue.getCompany());
        customer.setWebsite(clue.getWebsite());
        customer.setPhone(clue.getPhone());
        customer.setCreateBy(user.getId());
        customer.setCreateTime(DateUtils.formateDateTime(new Date()));
        customer.setAddress(clue.getAddress());
        customer.setContactSummary(clue.getContactSummary());
        customer.setNextContactTime(clue.getNextContactTime());
        if (customer!=null){
            customerMapper.insertCustomer(customer);
        }
//        联系人的转换
        Contacts contacts = new Contacts();
        contacts.setId(UUID.randomUUID().toString().replaceAll("-",""));
        contacts.setOwner(user.getId());
        contacts.setAddress(clue.getAddress());
        contacts.setAppellation(clue.getAppellation());
        contacts.setContactSummary(clue.getContactSummary());
        contacts.setNextContactTime(clue.getNextContactTime());
        contacts.setCreateBy(user.getId());
        contacts.setCreateTime(DateUtils.formateDateTime(new Date()));
        contacts.setCustomerId(customer.getId());
        contacts.setDescription(clue.getDescription());
        contacts.setEmail(clue.getEmail());
        contacts.setFullname(clue.getFullname());
        contacts.setJob(clue.getJob());
        contacts.setMphone(clue.getMphone());
        contacts.setSource(clue.getSource());
        if (contacts!=null){
            contactsMapper.insertContacts(contacts);
        }
//        线索备注转换到客户备注和联系人备注
        List<ClueRemark> clueRemarkList = clueRemarkService.selectClueRemarkByClueId(clueId);
        List<CustomerRemark> customerRemarkList = new ArrayList<>();
        List<ContactsRemark> contactsRemarkList = new ArrayList<>();


        for(ClueRemark clueRemark:clueRemarkList){
            CustomerRemark  customerRemark = new CustomerRemark();
            ContactsRemark contactsRemark = new ContactsRemark();
            customerRemark.setId(UUID.randomUUID().toString().replaceAll("-",""));
            customerRemark.setNoteContent(clueRemark.getNoteContent());
            customerRemark.setCreateBy(clueRemark.getCreateBy());
            customerRemark.setCreateTime(clueRemark.getCreateTime());
            customerRemark.setEditBy(clueRemark.getEditBy());
            customerRemark.setEditTime(clueRemark.getEditTime());
            customerRemark.setEditFlag(clueRemark.getEditFlag());
            customerRemark.setCustomerId(customer.getId());
            customerRemarkList.add(customerRemark);

            contactsRemark.setId(UUID.randomUUID().toString().replaceAll("-",""));
            contactsRemark.setNoteContent(clueRemark.getNoteContent());
            contactsRemark.setCreateBy(clueRemark.getCreateBy());
            contactsRemark.setCreateTime(clueRemark.getCreateTime());
            contactsRemark.setEditBy(clueRemark.getEditBy());
            contactsRemark.setEditTime(clueRemark.getEditTime());
            contactsRemark.setEditFlag(clueRemark.getEditFlag());
            contactsRemark.setContactsId(contacts.getId());
            contactsRemarkList.add(contactsRemark);
        }
        if (customerRemarkList!=null&&customerRemarkList.size()>0){
            customerRemarkService.insertCustomerByList(customerRemarkList);
        }
        if (contactsRemarkList!=null&&contactsRemarkList.size()>0){
            contactsRemarkService.insertContactsRemarkByContactsRemarkList(contactsRemarkList);
        }
//     线索与市场活动关联关系转换为联系人与市场活动的关联关系
        List<ClueActivityRelation> clueActivityRelationList = clueActivityRelationService.selectClueActivityRelationByClueId(clueId);
        List<ContactsActivityRelation> contactsActivityRelationList = new ArrayList<>();
        if(clueActivityRelationList!=null&&clueActivityRelationList.size()>0){
            ContactsActivityRelation car = null;
            for(ClueActivityRelation clueActivityRelation:clueActivityRelationList){
                car =new ContactsActivityRelation();
                car.setId(UUID.randomUUID().toString().replaceAll("-",""));
                car.setActivityId(clueActivityRelation.getActivityId());
                car.setContactsId(contacts.getId());
                contactsActivityRelationList.add(car);
            }
            contactsActivityRelationService.insertContactsActivityRelationByList(contactsActivityRelationList);
        }

//       创建交易
        if ("true".equalsIgnoreCase(isCreateTran)){
            Tran tran = new Tran();
            tran.setId(UUID.randomUUID().toString().replaceAll("-",""));
            tran.setCreateBy(user.getId());
            tran.setCreateTime(DateUtils.formateDateTime(new Date()));
            tran.setMoney((String)map.get("money"));
            tran.setActivityId((String)map.get("activityId"));
            tran.setCustomerId(customer.getId());
            tran.setExpectedDate((String)map.get("expectedDate"));
            tran.setStage((String) map.get("stage"));
            tran.setName((String) map.get("name"));
            tran.setOwner(user.getId());
            tran.setContactsId(contacts.getId());
            tranService.insertTran(tran);
//            将线索中的备注批量写入交易备注表中
            List<TranRemark> tranRemarkList = new ArrayList<>();
            TranRemark tranRemark = null;
            if (clueRemarkList.size()>0&&clueRemarkList!=null){
                for (ClueRemark clueRemark:clueRemarkList){
                    tranRemark = new TranRemark();
                    tranRemark.setId(UUID.randomUUID().toString().replaceAll("-",""));
                    tranRemark.setNoteContent(clueRemark.getNoteContent());
                    tranRemark.setCreateBy(clueRemark.getCreateBy());
                    tranRemark.setCreateTime(clueRemark.getCreateTime());
                    tranRemark.setEditBy(clueRemark.getEditBy());
                    tranRemark.setEditTime(clueRemark.getEditTime());
                    tranRemark.setEditFlag(clueRemark.getEditFlag());
                    tranRemark.setTranId(tran.getId());
                    tranRemarkList.add(tranRemark);
                }
                tranRemarkMapper.insertTranRemarkByList(tranRemarkList);
            }
        }

//        删除该线索下的所有备注
          clueRemarkService.deleteClueRemarkByClueId(clueId);

//        删除该线索关联的市场活动关系
          clueActivityRelationService.deleteClueActivityRelationByClueId(clueId);
//        删除该线索
          clueMapper.deleteClueByClueId(clueId);
    }


}
